#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
@File    :   handan_tuils.py    
@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2022/4/22 0022 16:10   st      1.0         None
'''
import json
from string import ascii_lowercase, ascii_uppercase

import numpy as np
from collections import Counter
from similar.sim_cos_utils import similarity_cosine
from utils.constent import *
from utils.handian_data import HandianData


class BiHua:
    def __init__(self):
        self.load_everything()

    def load_everything(self):
        with open(os.path.join(stroke2vec_dir, 'char_with_num_and_stroke.json'), 'r', encoding='utf-8') as fp:
            self.char_with_num_and_stroke = json.loads(fp.read())
        with open(os.path.join(stroke2vec_dir, 'char_with_num_and_strokeid.json'), 'r', encoding='utf-8') as fp:
            self.char_with_num_and_strokeid = json.loads(fp.read())

    def get_char_with_radical_and_num_and_stroke(self, char):
        return self.char_with_num_and_stroke.get(char)

    # def get_char_with_radical_and_num_and_strokeid(self, char):
    #     return self.char_with_num_and_strokeid[char]


def load_bishun_voc(char, voc_dmi):
    """
    字符转换笔顺向量
    :param char:
    :param voc_dmi:
    :return:
    """
    vec = np.zeros((voc_dmi,))
    res = HandianData.get_bishun_seq_cached(char)
    for i, x in enumerate(res):
        if i >= voc_dmi:
            break
        vec[i] = int(x)
    return vec


def load_pinyin_voc(char, voc_dmi=32):
    '''
    把字转换成拼音向量
    :param char:
    :param voc_dmi:
    :return:
    '''
    vec = np.zeros((voc_dmi,))
    pinyin_str = HandianData.get_pinyin_seq_cached(char).lower()
    for pinyin in pinyin_str.split(';'):
        if not pinyin:
            continue
        shengdiao = int(pinyin[-1])
        cnts = Counter(pinyin)
        for c in cnts:
            if c not in ascii_lowercase:
                continue
            i = ascii_lowercase.index(c)
            if i >= voc_dmi:
                break
            vec[i] = cnts[c]
        vec[-1] = shengdiao
    return vec


def load_wubi_voc(char, voc_dmi=32):
    """
    转换成五笔
    :param char:
    :param voc_dmi:
    :return:
    """
    vec = np.zeros((voc_dmi, ))
    wubi = HandianData.get_wubi_seq_cached(char)
    cnts = Counter(wubi)
    for c in cnts:
        if c not in ascii_uppercase:
            continue
        i = ascii_uppercase.index(c)
        if i >= voc_dmi:
            break
        vec[i] = cnts[c]
    return vec


def load_zhengma_voc(char, voc_dmi=32):
    """
    转换成郑码
    :param char:
    :param voc_dmi:
    :return:
    """
    vec = np.zeros((voc_dmi, ))
    zhengma = HandianData.get_zhengma_seq_cached(char)
    cnts = Counter(zhengma)
    for c in cnts:
        if c not in ascii_uppercase:
            continue
        i = ascii_uppercase.index(c)
        if i >= voc_dmi:
            break
        vec[i] = cnts[c]
    return vec


def vector(word, voc_dmi=32):
    vector1 = np.zeros(voc_dmi*4)
    for char in word:
        bs = load_bishun_voc(char, voc_dmi)
        wb = load_wubi_voc(char, voc_dmi)
        zm = load_zhengma_voc(char, voc_dmi)
        py = load_pinyin_voc(char, voc_dmi)
        # 拼接向量
        char_voc = np.concatenate((py, bs, wb, zm))
        vector1 += char_voc
    return vector1 / len(word)


def get_handian_sim(word1, word2):
    """
    汉典相似度计算
    :param word1:
    :param word2:
    :return:
    """
    voc_1 = vector(word1)
    voc_2 = vector(word2)
    sim = similarity_cosine(voc_1, voc_2)
    return sim


# print(get_handian_sim('清华', '清华大学'))
